package com.t17;

import java.util.*;

/**
 * Created by xuyiwei on 2018/8/21.
 */
public class SlowMap<K,V> extends AbstractMap<K,V> {
    private List<K> keys = new ArrayList<K>();
    private List<V> values = new ArrayList<V>();
    public V put(K key,V value){
        V oldValue = get(key);
        if(!keys.contains(key)){
            keys.add(key);
            values.add(value);
        }else{
            values.add(keys.indexOf(key),value);
        }
        return oldValue;
    }
    public V get(Object key){
        if(!keys.contains(key)){
            return null;
        }
        return values.get(keys.indexOf(key));
    }
    private EntrySet entrySet = new EntrySet();
    public Set<Entry<K,V>> entrySet(){
        return entrySet;
    }
    private class EntrySet extends AbstractSet<Map.Entry<K,V>>{
        @Override
        public Iterator<Entry<K, V>> iterator() {
            return new Iterator<Entry<K, V>>() {
                private int index = -1;
                boolean canRemove;
                @Override
                public boolean hasNext() {
                    return index<keys.size()-1;
                }

                @Override
                public Entry<K, V> next() {
                    canRemove = true;
                    ++index;
                    return new MapEntry<K, V>(keys.get(index),values.get(index));
                }
                public void remove(){
                    if(!canRemove){
                        throw new IllegalStateException();
                    }
                    canRemove = false;
                    keys.remove(index);
                    values.remove(index--);
                }
            };
        }
        @Override
        public boolean contains(Object o) {
            if(o instanceof MapEntry){
                MapEntry<K,V> e = (MapEntry<K, V>) o;
                K key = e.getKey();
                if(keys.contains(key)){
                    return e.equals(new MapEntry<K, V>(key,get(key)));
                }
            }
            return false;
        }
        @Override
        public boolean remove(Object o) {
            if(contains(o)){
                MapEntry<K,V> e = (MapEntry<K, V>) o;
                K key = e.getKey();
                V val = e.getValue();
                keys.remove(key);
                values.remove(val);
                return true;
            }
            return false;
        }
        @Override
        public int size() {
            return keys.size();
        }
        public void clear(){
            keys.clear();
            values.clear();
        }
    }
    public static void main(String[] args) {
        SlowMap<String,String> slowMap = new SlowMap<>();
        slowMap.putAll(Countries.capitals(15));
        System.out.println(slowMap);
        System.out.println(slowMap.get("BENIN"));
        System.out.println(slowMap.entrySet());
        for(Entry<String,String> entry:slowMap.entrySet()){
            System.out.println(entry.getKey());
            System.out.println(entry.getValue());
        }
    }
}
